package com.xckj.teacher.presenter;

import androidx.annotation.NonNull;

import com.xckj.teacher.bean.BaseBean;
import com.xckj.teacher.bean.SearchBookBean;
import com.xckj.teacher.framework.net.RetrofitManager;
import com.xckj.teacher.global.Constant;
import com.xckj.teacher.iview.ISearchBookView;

import java.util.List;
import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @Author: ChenYao
 * @Describe: 搜索书籍
 * @Date: 2024/4/12 16:16
 */
public class SearchBookPresenter {
    private final ISearchBookView searchBookView;

    public SearchBookPresenter(ISearchBookView searchBookView) {
        this.searchBookView = searchBookView;
    }

    public void searchBook(String key, int page, int pageSize) {
        searchBookView.showLoading();
        Call<BaseBean<List<SearchBookBean>>> call = RetrofitManager.getInstance().getService().searchBook(key, page, pageSize);
        call.enqueue(new Callback<BaseBean<List<SearchBookBean>>>() {
            @Override
            public void onResponse(@NonNull Call<BaseBean<List<SearchBookBean>>> call, @NonNull Response<BaseBean<List<SearchBookBean>>> response) {
                if (response.isSuccessful()) {
                    BaseBean<List<SearchBookBean>> baseBean = response.body();
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        searchBookView.success(baseBean.data);
                    } else {
                        searchBookView.failed(baseBean.msg);
                    }
                } else {
                    searchBookView.failed("请求失败");
                }
                searchBookView.hideLoading();
            }

            @Override
            public void onFailure(@NonNull Call<BaseBean<List<SearchBookBean>>> call, @NonNull Throwable t) {
                t.printStackTrace();
                searchBookView.failed("请求失败");
                searchBookView.hideLoading();
            }
        });
    }
}
